import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import ElementUI from 'element-ui'

Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import(/* webpackChunkName: "about" */ '../views/Register.vue'),
    },
    {
        path: '/404',
        name: '404',
        component: () => import(/* webpackChunkName: "about" */ '../views/404.vue'),
    },
    {
        path: '/front',
        name: 'front',
        redirect: './front/home',
        component: () => import(/* webpackChunkName: "about" */ '../views/front/Front.vue'),
        children: [
            {
                path: 'home',
                name: 'home',
                component: () => import(/* webpackChunkName: "about" */ '../views/front/Home.vue'),
            },
            {
                path: 'password',
                name: 'password',
                component: () => import(/* webpackChunkName: "about" */ '../views/front/Password.vue'),
            },
            {
                path: 'person',
                name: 'person',
                component: () => import(/* webpackChunkName: "about" */ '../views/front/Person.vue'),
            },
        ]
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

//重置路由器里面的路由集合
export const resetRoutes = () => {
    router.matcher = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
    })
}

// 路由重复点击报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export const setRoutes = () => {
    //获取浏览器缓存的菜单数据
    const localMenus = localStorage.getItem("menus");
    if (localMenus) {
        const currentRoutes = router.getRoutes().map(v => v.name);
        if (!currentRoutes.includes('manage')) {
            //当前Router不包含manage，在拼装
            const manageRoute = {
                path: '/',
                name: 'manage',
                redirect: '/home',
                component: () => import(/* webpackChunkName: "about" */ '../views/Manage.vue'),
                children: [
                    {
                        path: 'person',
                        name: '个人信息',
                        component: () => import(/* webpackChunkName: "about" */ '../views/Person.vue'),
                    },
                    {
                        path: 'password',
                        name: '修改密码',
                        component: () => import(/* webpackChunkName: "about" */ '../views/Password.vue'),
                    }
                ]
            };
            const menus = JSON.parse(localMenus);
            menus.forEach(item => {
                if (item.path) {
                    const itemMenu = {
                        path: item.path.replace("/", ""),
                        name: item.name,
                        component: () => import(/* webpackChunkName: "about" */ '../views/admin/' + item.pagePath + '.vue'),
                    };
                    manageRoute.children.push(itemMenu);
                } else if (item.children.length) {
                    item.children.forEach(item => {
                        const itemMenu = {
                            path: item.path.replace("/", ""),
                            name: item.name,
                            component: () => import(/* webpackChunkName: "about" */ '../views/admin/' + item.pagePath + '.vue'),
                        };
                        manageRoute.children.push(itemMenu);
                    })
                }
            })
            router.addRoute(manageRoute);
            console.log('1---重新加入manage路由')
            console.log('1---重新加入manage路由')
            console.log('1---重新加入manage路由')
            console.log(router.getRoutes())
        } else {
            console.log('未重新加入manage路由')
            console.log('未重新加入manage路由')
            console.log('未重新加入manage路由')
            console.log(router.getRoutes())
            debugger
        }
    }
}

setRoutes()

router.beforeEach((to, from, next) => {
    localStorage.setItem('currentPathName', to.name);
    store.commit('setPath')
    const localMenus = localStorage.getItem("menus");
    if (!to.matched.length) {
        //没有匹配到路由（也就是未找到路由)
        if (localMenus) {
            //用户登录了
            next('/404')
        } else {
            ElementUI.Message({
                message: '请先登录',
                type: 'warning'
            });
            next('/login')
        }
    }

    // 校验用户个人信息是否完善
    if (from.path === '/login' || from.path === '/register' || to.path === '/login') {
        next()
    } else if(to.path !== "/Person") {
       checkUserInfo()
    }
    next();
})

async function checkUserInfo() {
    let id = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')).id : null;
    if (id) {
        request.get("/user/findById/" + id).then(res => {
            if (res.code === '200') {
                for (const key in res.data) {
                    if (!res.data[key]) {
                        ElementUI.MessageBox.alert('请先完善个人信息', "提示", {
                            type: 'warning',
                        }).then(() => {
                            router.push('/Person')
                        });
                        return;
                    }
                }
            }
        })
    }
}

export default router
